package org.dna.wicketui;

import java.util.Arrays;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.dna.uimodel.MenuItem;
import org.dna.wicketui.activities.ManageActivityPage;
import org.dna.wicketui.issues.ManageIssuePage;
import org.dna.wicketui.users.ManageUserPage;

public class MenuPanel extends Panel {

    private static class GlobalMenuItem extends MenuItem {

        GlobalMenuItem(String caption, Class<? extends MetroPage> dest) {
            super(caption, dest);
        }

        @Override
        public Component createMenu() {
            Link link = new Link("link") {

                @Override
                public void onClick() {
                    getParent().add(new SimpleAttributeModifier("id", "current"));
                    setResponsePage(m_destination);
                }
            };
            link.add(new Label("caption", getCaption()));
            return link;
        }
    }
    private static final long serialVersionUID = 1L;

    @SuppressWarnings({"serial", "unchecked"})
    public MenuPanel(String id) {
        super(id);

        MenuItem activitiesMenu = new GlobalMenuItem("Activities", ManageActivityPage.class);
        MenuItem usersMenu = new GlobalMenuItem("Users", ManageUserPage.class);
        MenuItem issueMenu = new GlobalMenuItem("Issues", ManageIssuePage.class);

        List<MenuItem> menu = Arrays.asList(activitiesMenu, usersMenu, issueMenu);

        ListView lv = new ListView("menu", menu) {

            @Override
            protected void populateItem(ListItem item) {
                MenuItem menuitem = (MenuItem) item.getModelObject();
                item.add(menuitem.createMenu());
            }
        };
        add(lv);
    }
}
